package org.chen.gateway.exception;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.SaTokenException;
import lombok.extern.slf4j.Slf4j;
import org.chen.gateway.dto.R;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 捕获 NotLoginException 异常
     * @param e NotLoginException
     * @return R
     */
    @ExceptionHandler(NotLoginException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED) // 返回401状态码
    public R handleNotLoginException(NotLoginException e) {
        log.warn("用户未登录异常: {}", e.getMessage());
        
        // 根据异常类型返回不同的错误信息
        String message;
        switch (e.getType()) {
            case NotLoginException.NOT_TOKEN:
                message = "未提供Token";
                break;
            case NotLoginException.INVALID_TOKEN:
                message = "Token无效";
                break;
            case NotLoginException.TOKEN_TIMEOUT:
                message = "登录信息已过期，请重新登录";
                break;
            case NotLoginException.BE_REPLACED:
                message = "您的账户已在另一台设备上登录，如非本人操作，请立即修改密码";
                break;
            case NotLoginException.KICK_OUT:
                message = "已被系统强制下线";
                break;
            default:
                message = "登录状态异常，请重新登录";
        }
        
        return R.builder()
                .code(401)
                .msg(message)
                .data(null)
                .build();
    }

    /**
     * 捕获其他Sa-Token相关异常
     */
    @ExceptionHandler(SaTokenException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public R handleSaTokenException(SaTokenException e) {
        log.warn("Sa-Token异常: {}", e.getMessage());
        return R.builder()
                .code(401)
                .msg("认证异常：" + e.getMessage())
                .data(null)
                .build();
    }

    /**
     * 捕获业务异常
     */
    @ExceptionHandler(BusinessException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public R handleBusinessException(BusinessException e) {
        log.warn("业务异常: {}", e.getMessage());
        return R.builder()
                .code(400)
                .msg(e.getMessage())
                .data(null)
                .build();
    }

    /**
     * 捕获参数校验异常
     */
    @ExceptionHandler(IllegalArgumentException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public R handleIllegalArgumentException(IllegalArgumentException e) {
        log.warn("参数异常: {}", e.getMessage());
        return R.builder()
                .code(400)
                .msg("参数错误：" + e.getMessage())
                .data(null)
                .build();
    }

    /**
     * 捕获所有其他异常
     */
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public R handleException(Exception e) {
        log.error("系统异常: ", e);
        return R.builder()
                .code(500)
                .msg("系统内部错误")
                .data(null)
                .build();
    }
}